home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 6124 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.9 KB  |  106 lines

  1. Newsgroups: comp.lang.c++
  2. Path: in1.uu.net!iglou!news
  3. From: "Abe L. Getchell" <panther@iglou.com>
  4. Subject: BC++ Inheritance Question...
  5. X-Nntp-Posting-Host: dp-2-63.iglou.net
  6. Content-Type: text/plain; charset=us-ascii
  7. Message-ID: <311D9DBE.9E5@iglou.com>
  8. Sender: news@iglou.com (News Administrator)
  9. Content-Transfer-Encoding: 7bit
  10. Organization: 3DX Studios
  11. Mime-Version: 1.0
  12. Date: Sun, 11 Feb 1996 07:41:50 GMT
  13. X-Mailer: Mozilla 2.0 (Win16; I)
  14.  
  15. I am new to OOP and have a question about a block of code that
  16. I wrote and was having problems with.  In public inheritance, the private
  17. data mambers of a class stay private to the base class, and are not inherited
  18. by the derived class.  Now, in this code, it appears that the derived class is
  19. inheriting the private data member "z" of the base class.  I could just be interperating
  20. it wrong, or programming it wrong, but I cannot figure it out...  please post to me here,
  21. or send mail to my address.  I appriciate any help.  (code and output given below)
  22.  
  23. -----------------------------------------------------------------------------------------
  24. //  Testing class inheritance
  25.  
  26. #include <iostream.h>
  27. #include <conio.h>
  28.  
  29. // Class definition & function definitions for ClassOne
  30. class ClassOne {
  31.     public:
  32.         ClassOne( int = 1, int = 2, int = 3 );
  33.         int geta( void ) { return a; };
  34.         int getb( void ) { return b; };
  35.         int getz( void ) { return z; };
  36.         int setz( void ) { z = 99 ; };
  37.     protected:
  38.         int a;
  39.         int b;
  40.     private:
  41.         int z;
  42. };
  43.  
  44. ClassOne::ClassOne( int x, int y, int q )
  45. {
  46.     a = x;
  47.     b = y;
  48.     z = q;
  49. }
  50.  
  51. // Class definition & function definitions for ClassTwo
  52. class ClassTwo : public ClassOne {
  53.     public:
  54.         ClassTwo( int = 4, int = 5 );
  55.         int getc( void ) { return c; };
  56.         int getd( void ) { return d; };
  57.     protected:
  58.         int c;
  59.         int d;
  60. };
  61.  
  62. ClassTwo::ClassTwo( int q, int p )
  63. {
  64.     c = q;
  65.     d = p;
  66. }
  67.  
  68. main()
  69. {
  70.     ClassOne oneObj;
  71.     ClassTwo twoObj;
  72.  
  73.     cout << "ClassOne a = " << oneObj.geta() << "\n";
  74.     cout << "ClassOne b = " << oneObj.getb() << "\n";
  75.     cout << "ClassOne z = " << oneObj.getz() << "\n";
  76.     cout << "ClassTwo a = " << twoObj.geta() << "(inherited from ClassOne)\n";
  77.     cout << "ClassTwo b = " << twoObj.getb() << "(inherited from ClassOne)\n";
  78.     cout << "ClassTwo c = " << twoObj.getc() << "\n";
  79.     cout << "ClassTwo d = " << twoObj.getd() << "\n";
  80.     cout << "ClassTwo z = " << twoObj.getz() << "\n";
  81.     oneObj.setz();
  82.     cout << "ClassTwo z = " << twoObj.getz() << "\n";
  83.     cout << "ClassOne z = " << oneObj.getz() << "\n";
  84.  
  85.     getche();
  86.  
  87.     return 0;
  88. }
  89. ---------------------------------------------------------------------------------------
  90. ---------------------------------------------------------------------------------------
  91. ClassOne a = 1
  92. ClassOne b = 2
  93. ClassOne z = 3
  94. ClassTwo a = 1 (inherited from ClassOne)
  95. ClassTwo b = 2 (inherited from ClassOne)
  96. ClassTwo c = 4
  97. ClassTwo d = 5
  98. ClassTwo z = 3
  99. ClassTwo z = 3
  100. ClassOne z = 99
  101. ----------------------------------------------------------------------------------------
  102.  
  103.     Thanks in advance...
  104.  
  105. Abe L. Getchell
  106.